/**
 * Crazy Icon
 *  License by CrazyYi.
 *  v1.0.0 20200206: 在单元格的右侧，添加一个图标，给图标添加点击事件。点击图标出现“弹出层”
 *  v1.1.0 20200221: 改成数组方式来保存所有的
 *
 */
layui.define(['table', 'jquery', 'form', 'layer', 'crazyTool'], function (exports) {
    const MOD_NAME = 'crazyIcon'

    const $ = layui.jquery

    let gElem = {};

    /**
     * 定义 全局icon
     * @type {{
     *      mode: string,
     *      highlight: highlight,
     *      _elem: undefined,
     *      get: get,
     *      icon: {
     *          filter: {input: string, default: string, select: string, same: boolean, checkbox: string, radio: string},
     *          edit: {input: string, default: string, select: string, same: boolean, checkbox: string, radio: string}
     *          },
     *      create: (function(*=, *=): string),
     *      _class: {filter: string, edit: string},
     *      nohas: nohas,
     *      has: has,
     *      type: undefined,
     *      config: {},
     *      render: render,
     *      }}
     */
    let gIcons = {
        type: undefined,
        mode: 'edit',
        icon: {
            edit: {
                input: 'layui-icon-edit',
                select: 'layui-icon-down',
                checkbox: 'layui-icon-addition',
                radio: 'layui-icon-down',
                default: 'layui-icon-edit',
                same: true,
            },
            filter: {
                input: 'layui-icon-search',
                select: 'layui-icon-down',
                checkbox: 'layui-icon-search',
                radio: 'layui-icon-down',
                default: 'layui-icon-search',
                same: true,
            },
        },
        get: function (type, mode) {
            let iconObj = this.icon[mode];
            if (iconObj.same) { return iconObj.default; } else {
                if (iconObj.hasOwnProperty(type)) {
                    return iconObj[type];
                } else {
                    return iconObj.default;
                }
            }
        },
        _class: {
            edit: 'crazy-table-edit',
            filter: 'crazy-table-show-icon',
            default: 'crazy-table-show-icon',
        },
        create: function (type, mode, id) {
            this.type = type;
            this.mode = mode = mode || 'edit';
            let _class = this._class.default; // this._class[mode];
            let _icon = this.get(type, mode);
            let _html = (`<span class="${_class} layui-inline"><i class="layui-icon ${_icon}"></i></span>`);
            this._elem[id] = $(_html);
            return _html;
        },

        config: {}, // 保存当前的配置
        _elem: {}, // 保存当前create的元素
        render: function (config, mode, value, id) {
            this.config = config;
            id = id || config.name;
            this.create(config.type, mode, id);
            if (!this._elem[id]) { return false}

            this.highlight(value, id);

            return this._elem[id];
        },
        highlight: function (value = false, name = config.name) {
            // name = name || config.name;
            // value = value || false;
            //图标默认高亮
            if (!!value) { this.has(name); } else { this.nohas(name); }
        },
        nohas: function (name) {
            // log.print('icon highlight: remove');
            !!this._elem[name] && $(this._elem[name]).removeClass("tableFilter-has"); // 取消 图标高亮
        },
        has: function (name) {
            !!this._elem[name] && $(this._elem[name]).addClass("tableFilter-has"); // 设置 图标高亮
        },
    };

    //输出接口
    exports(MOD_NAME, gIcons);
});